package com.reptile.service.impl;

import com.example.annotations.Autowired;
import com.reptile.model.title.model.ContentModel;
import com.reptile.model.title.model.TitleModel;
import com.reptile.model.title.model.vo.TitleVo;
import com.reptile.model.title.service.ContentModelService;
import com.reptile.model.title.service.TitleModelService;
import org.jsoup.Jsoup;
import com.example.annotations.Service;
import com.reptile.service.JsoupService;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname JsoupServiceImpl
 * @Description TODO
 * @Date 2022/8/10 20:06
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class JsoupServiceImpl implements JsoupService {

    @Autowired
    TitleModelService titleModelService;

    @Autowired
    ContentModelService contentModelService;



    @Override
    public void getSinaHotSearch() throws IOException {
        Document document = Jsoup.connect("https://s.weibo.com/top/summary?cate=socialevent")
                .cookie("cookie","SINAGLOBAL=1089464624227.2832.1644464465783; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhC8bv3Jbv-KxQVyG8zprll5JpX5KMhUgL.FoqNehnc1KMNS022dJLoIpqLxK-LBo5L12qLxK.L1-zL1hB4eK5R; ALF=1691670764; SSOLoginState=1660134766; SCF=AjPz5641PkLk-HaELQWmi9dd6A6zzPNYPTnYg08OFNTaoIisVio3CZBdyQOoL5XZWdsUiOgiIzwwFS5k0EBUbIY.; SUB=_2A25P99U_DeRhGeBJ61oX-SnLzD2IHXVshUH3rDV8PUNbmtANLWnQkW9NRkv4FkB3T3L1vG-YoSJZZj3_yBBoVMIu; _s_tentry=login.sina.com.cn; UOR=,,login.sina.com.cn; Apache=3300176940614.976.1660134765201; ULV=1660134765217:23:6:2:3300176940614.976.1660134765201:1659925603085")
                .get();

        String title = document.title();
        Elements list=document.getElementsByTag("tr");

        List<TitleVo> titleVoList=new ArrayList<>();

        for (int i = 0; i < list.size() ; i++) {
            Element element = list.get(i);
            Elements elements=element.getElementsByTag("a");
            for (int j = 0; j < elements.size() ; j++) {
                Element a=elements.get(j);
                String contentTitle=a.text();
                String href=a.attr("href");

                   System.out.println(contentTitle);
                    System.out.println(href);
                    String content=getSinaContent(href);
                    TitleVo titleVo=new TitleVo();
                    titleVo.setChannelId(8);
                    titleVo.setIsmarker(true);
                    titleVo.setContent(content);
                    titleVo.setTitle(contentTitle);
                    titleVo.setTag("标签");
                    titleVoList.add(titleVo);

            }
        }

        for (int i = 0; i < titleVoList.size() ; i++) {
            TitleVo titleVo=titleVoList.get(i);
            save(titleVo);
        }
    }


    public String getSinaContent(String href) throws IOException {
        StringBuffer stringBuffer=new StringBuffer();
        String baseHref="https://s.weibo.com";
        href=baseHref+href;
        Document document=Jsoup.connect(href)
                .cookie("cookie","SINAGLOBAL=1089464624227.2832.1644464465783; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhC8bv3Jbv-KxQVyG8zprll5JpX5KMhUgL.FoqNehnc1KMNS022dJLoIpqLxK-LBo5L12qLxK.L1-zL1hB4eK5R; ALF=1691670764; SSOLoginState=1660134766; SCF=AjPz5641PkLk-HaELQWmi9dd6A6zzPNYPTnYg08OFNTaoIisVio3CZBdyQOoL5XZWdsUiOgiIzwwFS5k0EBUbIY.; SUB=_2A25P99U_DeRhGeBJ61oX-SnLzD2IHXVshUH3rDV8PUNbmtANLWnQkW9NRkv4FkB3T3L1vG-YoSJZZj3_yBBoVMIu; _s_tentry=login.sina.com.cn; UOR=,,login.sina.com.cn; Apache=3300176940614.976.1660134765201; ULV=1660134765217:23:6:2:3300176940614.976.1660134765201:1659925603085")
                .get();
        Elements elements=document.getElementsByAttributeValue("class","txt");
        for (int i = 0; i < elements.size(); i++) {
            Element element=elements.get(i);
            if(!element.html().contains("收起")){
                System.out.println(element.html());
                stringBuffer.append(element.html());
            }

        }

        return stringBuffer.toString();
    }


    public void save(TitleVo titleVo){
       // UserModel userModel=userModelService.getUserModel();
        if(titleVo == null){
            return;
        }
        if(titleVo.getChannelId()==null ){
           return;
        }
        if(titleVo.getId()==null){
            ContentModel contentModel=new ContentModel();
            contentModel.setContent(titleVo.getContent());
            contentModelService.save(contentModel);
            TitleModel titleModel = new TitleModel();
            titleModel.setTitle(titleVo.getTitle());
            titleModel.setTag(titleVo.getTag());
            titleModel.setChannelId(titleVo.getChannelId());
            titleModel.setContentid(contentModel.getId());
            titleModel.setCreatedate(new Date());
            titleModel.setUid(106);
            titleModel.setIsmarker(titleVo.isIsmarker());
            titleModelService.save(titleModel);
        }
    }

}
